#ifndef __PWMDAC_H
#define __PWMDAC_H

#include "./SYSTEM/sys/sys.h"

/* -------------------------------------- PWM DAC 引脚 和 定时器 定义 -------------------------------------- */

/* PWMDAC1 - TIM1_CH4 */
#define PWMDAC1_GPIO_PORT                   GPIOE
#define PWMDAC1_GPIO_PIN                    GPIO_PIN_14
#define PWMDAC1_GPIO_CLK_ENABLE()           do{ __HAL_RCC_GPIOE_CLK_ENABLE(); }while(0)
#define PWMDAC1_GPIO_AFTIMX                 GPIO_AF1_TIM1

/* PWMDAC2 - TIM1_CH3 */
#define PWMDAC2_GPIO_PORT                   GPIOE
#define PWMDAC2_GPIO_PIN                    GPIO_PIN_13
#define PWMDAC2_GPIO_CLK_ENABLE()           do{ __HAL_RCC_GPIOE_CLK_ENABLE(); }while(0)
#define PWMDAC2_GPIO_AFTIMX                 GPIO_AF1_TIM1

/* PWMDAC3 - TIM1_CH2 */
#define PWMDAC3_GPIO_PORT                   GPIOE
#define PWMDAC3_GPIO_PIN                    GPIO_PIN_11
#define PWMDAC3_GPIO_CLK_ENABLE()           do{ __HAL_RCC_GPIOE_CLK_ENABLE(); }while(0)
#define PWMDAC3_GPIO_AFTIMX                 GPIO_AF1_TIM1

/* PWMDAC4 - TIM1_CH1 */
#define PWMDAC4_GPIO_PORT                   GPIOE
#define PWMDAC4_GPIO_PIN                    GPIO_PIN_9
#define PWMDAC4_GPIO_CLK_ENABLE()           do{ __HAL_RCC_GPIOE_CLK_ENABLE(); }while(0)
#define PWMDAC4_GPIO_AFTIMX                 GPIO_AF1_TIM1

/* 共用的TIM1定时器 */
#define PWMDAC_TIMX                         TIM1
#define PWMDAC_TIMX_CLK_ENABLE()            do{ __HAL_RCC_TIM1_CLK_ENABLE(); }while(0)

/* 各通道定义 */
#define PWMDAC1_TIMX_CHY                    TIM_CHANNEL_4
#define PWMDAC2_TIMX_CHY                    TIM_CHANNEL_3
#define PWMDAC3_TIMX_CHY                    TIM_CHANNEL_2
#define PWMDAC4_TIMX_CHY                    TIM_CHANNEL_1

/* PWMDAC通道编号定义 */
#define PWMDAC_CH1                          1   /* PWMDAC通道1 (TIM1_CH4) */
#define PWMDAC_CH2                          2   /* PWMDAC通道2 (TIM1_CH3) */
#define PWMDAC_CH3                          3   /* PWMDAC通道3 (TIM1_CH2) */
#define PWMDAC_CH4                          4   /* PWMDAC通道4 (TIM1_CH1) */

/* -------------------------------------------- 全局变量声明 ---------------------------------------------- */

extern TIM_HandleTypeDef g_tim1_handler;        /* 定时器1句柄 */
extern TIM_OC_InitTypeDef g_tim1_oc_handler;    /* 定时器1通道句柄 */

/* -------------------------------------------- 函数声明 -------------------------------------------------- */

/* PWM DAC初始化函数 */
void pwmdac_init(uint16_t arr, uint16_t psc);

/* 通用函数 - 可以指定通道 */
void pwmdac_set_duty(uint8_t channel, uint8_t duty);    /* 设置指定通道占空比，范围0-100% */
void pwmdac_set_voltage(uint8_t channel, uint16_t vol); /* 设置指定通道输出电压，范围0-10000mV */
void pwmdac_set_current_4_20ma(uint8_t channel, uint16_t curr); /* 设置PWMDAC3/4通道输出电流，范围0或4000-20000μA，0表示关闭输出 */

#endif
